u Calculate e

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 59433 Accepted Submission(s): 27248

Problem Description

A simple mathematical formula for e is

img

where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n.

Output

Output the approximations of e generated by the above formula for the values of n from 0 to 9. The beginning of your output should appear similar to that shown below.

Sample Output

n e
- -----------
0 1
1 2
2 2.5
3 2.666666667
4 2.708333333

Source

Greater New York 2000

Recommend

JGShining | We have carefully selected several similar problems for you: 1013 1021 1017 1020 1014

Accepted Code

//难在格式,不是算法
package cn.edu.hdu.acm;

public class Main1012 {
    public static void main(String[] args) {
        System.out.println("n e");
        System.out.println("- -----------");
        double[] arr = new double[10];
        for(int i = 0;i<10;i++) {
            arr[i] = factorial(i);
        }
        System.out.println("0 1");
        System.out.println("1 2");
        System.out.println("2 2.5");
        for(int i = 3;i<10;i++) {
            System.out.printf("%d %.9f", i, sigma(i, arr));
            System.out.println();
        }
    }

    //求阶乘
    public static double factorial(int num) {
        double res = 1;
        while(num>1) {
            res *= num;
            num--;
        }
        return 1f/res;
    }

    //求和
    public static double sigma(int num, double[] arr) {
        double res = 0f;
        for(int i = 0;i<=num;i++) {
            res += arr[i];
        }
        return res;
    }
}

results matching ""

    No results matching ""